import { axiosProductIns } from '../axios';

const URL: Record<keyof IService.Product.HTTP, string> = {
    getProfileList: '/profile/page',
    getProfileAllList: 'profile/list',
    createProfile: '/profile/add',
    deleteProfile: '/profile/delete',
    getProfileUploadUrl: '/profile/file/getHWYunUploadAuthUrlAndFileId',
    getProfileDownloadUrl: '/profile/file/getFileDownloadAuthUrlHUAWEI',
    updateProfileFile: '/profile/file/upload',
    getCategoryTree: '/pro-category-dict/tree',
    getCategoryDetails: '/pro-category/detailByDictId',
    getCategoryIcon: '/pro-category/iconDownload',
    updateCategoryIcon: '/pro-category/geFileUploadIconId',
    createCategory: '/pro-category/add',
    updateCategory: 'pro-category/edit',
};

export async function getProfileList(request: IService.Product.HTTP['getProfileList']['request']): Promise<IService.Product.HTTP['getProfileList']['response']> {
    return axiosProductIns.post(URL.getProfileList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function getProfileAllList(request: IService.Product.HTTP['getProfileAllList']['request']): Promise<IService.Product.HTTP['getProfileAllList']['response']> {
    return axiosProductIns.post(URL.getProfileAllList, request);
}

export async function createProfile(request: IService.Product.HTTP['createProfile']['request']): Promise<IService.Product.HTTP['createProfile']['response']> {
    return axiosProductIns.post(URL.createProfile, request);
}

export async function deleteProfile(request: IService.Product.HTTP['deleteProfile']['request']): Promise<IService.Product.HTTP['deleteProfile']['response']> {
    return axiosProductIns.post(URL.deleteProfile, request);
}

export async function getProfileUploadUrl(request: IService.Product.HTTP['getProfileUploadUrl']['request']): Promise<IService.Product.HTTP['getProfileUploadUrl']['response']> {
    return axiosProductIns.post(URL.getProfileUploadUrl, request);
}

export async function getProfileDownloadUrl(request: IService.Product.HTTP['getProfileDownloadUrl']['request']): Promise<IService.Product.HTTP['getProfileDownloadUrl']['response']> {
    return axiosProductIns.post(URL.getProfileDownloadUrl, request);
}

export async function updateProfileFile(request: IService.Product.HTTP['updateProfileFile']['request']): Promise<IService.Product.HTTP['updateProfileFile']['response']> {
    const formData = new FormData();
    formData.append('fileId', request.fileId);
    formData.append('file', request.file);
    return axiosProductIns.post(URL.updateProfileFile, request, { headers: { 'Content-Type': 'multipart/form-data' } });
}

export async function getCategoryTree(request: IService.Product.HTTP['getCategoryTree']['request']): Promise<IService.Product.HTTP['getCategoryTree']['response']> {
    return axiosProductIns.post(URL.getCategoryTree, request);
}

export async function getCategoryDetails(request: IService.Product.HTTP['getCategoryDetails']['request']): Promise<IService.Product.HTTP['getCategoryDetails']['response']> {
    return axiosProductIns.get(URL.getCategoryDetails, { params: request });
}

export async function getCategoryIcon(request: IService.Product.HTTP['getCategoryIcon']['request']): Promise<IService.Product.HTTP['getCategoryIcon']['response']> {
    return axiosProductIns.get(URL.getCategoryIcon, { params: request, responseType: 'arraybuffer' });
}

export async function updateCategoryIcon(request: IService.Product.HTTP['updateCategoryIcon']['request']): Promise<IService.Product.HTTP['updateCategoryIcon']['response']> {
    const formData = new FormData();
    formData.append('file', request.file);
    return axiosProductIns.post(URL.updateCategoryIcon, request, { headers: { 'Content-Type': 'multipart/form-data' } });
}

export async function createCategory(request: IService.Product.HTTP['createCategory']['request']): Promise<IService.Product.HTTP['createCategory']['response']> {
    return axiosProductIns.post(URL.createCategory, request);
}

export async function updateCategory(request: IService.Product.HTTP['updateCategory']['request']): Promise<IService.Product.HTTP['updateCategory']['response']> {
    return axiosProductIns.post(URL.updateCategory, request);
}
